var changeNum= 0,wxDepartment_id='',wcAisle_id='',wcShelf_id='',pager= 1,sort="popularity",noDatePager=true;
var wineDas ='',shopDas='';
$(function(){
	wineDas = MPJ.getWineData();
	shopDas = MPJ.getShopData();
	var wine_index=sessionStorage.getItem('wine_index');
	if(wine_index != undefined && wine_index != "" && wine_index != null){
		$('.GreenHeader .btnBox a').removeClass('active');
		$('.GreenHeader .btnBox a').eq(wine_index).addClass('active');
		$('.mainContent .page').hide();
		$('.mainContent .page').eq(wine_index).show();
	}

	getTopPicks();//获取getTopPicks数据
	getNewArrival();//获取NewArrival数据
	getOneCategory();//获取所有酒分类
	getAllWine(1);
	$('#offCanvasContentScroll .mui-scroll').css('transform', 'translate3d(0px, 0px, 0px) translateZ(0px)');
})
//获取getTopPicks数据
function getTopPicks(){
	var arr={
		hf_s_id:"WD11",
		hf_srv_id:"Pv_jpdt_get_all_ddc",
		hs_action_id:"submit",
		hs_srch_item_no:shopDas.Wine.TopPicks.replace(/,/g,' '),
		hf_locale_id:"en"
	}
	getAjaxRequest(arr, function (result) {
		var str = setProductList_index(result);
		$('.winCor .slideBox.vertical-list ul').html(str[0]);
		slideBox();//内容可以左右滚动
	})
}
//获取NewArrival数据
function getNewArrival(){
	var arr={
		hf_s_id:"WD11",
		hf_srv_id:"Pv_jpdt_get_all_ddc",
		hs_action_id:"submit",
		hs_srch_item_no:shopDas.Wine.NewArrival.replace(/,/g,' '),
		hf_locale_id:"en"
	}
	getAjaxRequest(arr, function (result) {
		var str = setProductList_index(result);
		$('.winCor .content.vertical-list ul').html(str[0]);
	})
}
//内容可以左右滚动
function slideBox(){
	var p=1;
	var $this=$('.slideBox');
	var pLen=$('.slideBox li').length;
	var pWidth=$(window).width()-10;
	if(pWidth>630){
		pWidth=630;
	}
	var tWidth=pWidth*pLen/2;
	$('.slideBox ul').css({width:tWidth});
	$('.slideBox li').each(function(){
		$(this).css({width:pWidth/2});
	})

	$this.find('.controll-btn.next').on("tap",function(){
		p=p+1;
		if(p>Math.ceil(pLen/2)){
			p=1;
		}
		goPage(p);
	})
	$this.find('.controll-btn.prev').on("tap",function(){
		p=p-1;
		if(p<1){
			p=Math.ceil(pLen/2);
		}
		goPage(p);
	})


	function goPage(p){
		if(p<pLen){
			$('.slideBox ul').animate({marginLeft:-(pWidth*(p-1))+'px'});
		}else{
			p=1;
			$('.slideBox ul').animate({marginLeft:'0'});
		}
	}
}
//获取选中的分类名
function  getCategory_sub(){
	var wineMenu_Arr=JSON.parse(sessionStorage.getItem("wineMenu_Arr"));
	if(wineMenu_Arr != undefined && wineMenu_Arr !="" && wineMenu_Arr != null){
		var navigation='';
		for (var i=0;i<wineMenu_Arr.length;i++) {
			if (i == 0) {
				wxDepartment_id=wineMenu_Arr[i].id;
				navigation+='<a href="javascript:void(0)" class="fresh-fruits mui-ellipsis" data-id="'+wineMenu_Arr[i].id+'">'+
					'<img src="'+MPJ.base+'/resources/images/icon/pro-apple-icon.png" />'+wineMenu_Arr[i].name+
					'</a>';
			} else if( i == 1 ){
				wcAisle_id=wineMenu_Arr[i].id;
				navigation+='<a href="javascript:void(0)" class="two-fruits mui-ellipsis" data-id="'+wineMenu_Arr[i].id+'">'+
					'<span>&middot;</span>'+wineMenu_Arr[i].name+
					'</a>';
			} else if ( i == 2 ){
				wcShelf_id=wineMenu_Arr[i].id;
				navigation+='<a href="javascript:void(0)" class="three-fruits mui-ellipsis" data-id="'+wineMenu_Arr[i].id+'">'+
					'<span>&middot;</span>'+wineMenu_Arr[i].name+
					'</a>';
			}
		}
		$('.classify-name').html(navigation);
		set_map_width();//设置菜单宽度
		$('.fresh-fruits').on('tap',function(){
			wineMenu_Arr.splice(1,2);
			sessionStorage.setItem('wineMenu_Arr',JSON.stringify(wineMenu_Arr));
			$('.allWine .content.vertical-list ul').html('');
			changeNum=0;
			getOneCategory();
			getCategory_sub();
			getAllWine(1);
		})
		$('.two-fruits').on('tap',function(){;
			wineMenu_Arr.splice(2,2);
			sessionStorage.setItem('wineMenu_Arr',JSON.stringify(wineMenu_Arr));
			$('.allWine .content.vertical-list ul').html('');
			changeNum=2;
			getOneCategory();
			getCategory_sub();
			getAllWine(1);
		})
	}
}
//设置菜单宽度
function set_map_width(){
	var sWidth=$('.classify-name').width();
	var len=$('.classify-name a').length;
	var len1='',len2='',len3='';
	for (var i=0;i<len;i++) {
		if(i==0){
			len1=$('.classify-name a.fresh-fruits').width();
		}else if(i==1){
			len2=$('.classify-name a.two-fruits').width();
		}else if(i==2){
			len3=$('.classify-name a.three-fruits').width();
		}
	}
	if(len==2){
		if((len1+len2+60)>sWidth){
			$('.classify-name a.fresh-fruits').css("width",(sWidth-len2-30));
		}
	}
	if(len==3){
		if((len1+len2+len3+35)>sWidth){
			$('.classify-name a.fresh-fruits').css("width",(sWidth-len3-35)/2);
			$('.classify-name a.two-fruits').css("width",(sWidth-len3-35)/2);
		}
	}
}
//获取所有酒
function  getAllWine(pager){
	var arr={
		hf_s_id:"WD11",
		hf_srv_id:"Pv_jpdt_brw",
		hs_action_id:"submit",
		hs_wc_dept_id:wxDepartment_id,
		hs_wc_aisle_id:wcAisle_id,
		hs_wc_shelf_id:wcShelf_id,
		hs_rec_per_page:20,
		hs_sort:sort,
		hs_srch_page_no:pager
	}
	getAjaxRequest(arr, function (result) {
		var str=setProductList_ver(result);
		if(str[1]==0){
			noDatePager=false;
			$('.allWine .content.vertical-list ul').append("<li class='nodate' style='width: 100%'>No more data</li>");
		}else{
			$('.allWine .content.vertical-list ul').append(str[0]);
		}
		if(pager==1){
			$('#offCanvasContentScroll .mui-scroll').css('transform', 'translate3d(0px, 0px, 0px) translateZ(0px)');
		}


		//两列数据的列表点击
		$('.vertical-list li').each(function(){
			var $this=$(this);
			$this.on('tap','.imgBox',function(){
				var pdt_id=$this.data('id');
				var item_no=$this.data('item-no');
				var product_info={};
				product_info.pdt_id=pdt_id;
				product_info.item_no=item_no;
				sessionStorage.setItem('product_info',JSON.stringify(product_info));
				location.href=MPJ.base+"/pages/product/product-detail.jsp";
			})
			$this.on('tap','.addCar',function(){
				var pdt_id=$this.data('id');
				var item_no=$this.data('item-no');
				var arr={
					hf_s_id:"WD11",
					hf_srv_id:"Av_jcart",
					hs_action_id:"add_all_to_cart",
					hvs_add_pdt_id:pdt_id,
					hvs_add_qty:$this.find('.mui-numbox-input').val(),
				};
				getAjaxRequest(arr,function(result){
					if(result.bs_result==BS_RESULT.OK){
						dialog('Message', 'Add to Shopping Cart', 1);
						var $dialog = $('#dialog');
						$dialog.show();
						$dialog.find('.comfirmBtn').one('click', function() {
							$dialog.hide();
							$dialog.remove();
							getCartSumCount();
						})
					}

				});

			})
		})
	})
}
//获取第一层分类
function getOneCategory(){
	var category='';
	for(var i=0; i<wineDas.WCDepartment.length;i++){
		category+='<option value="'+wineDas.WCDepartment[i].id+'">'+wineDas.WCDepartment[i].Name+'</option>';
	}
	$('#refine').html('<option value="">Refine</option>'+category);


}
//分类选择
var wineMenu_Arr=[];
$('#refine').change(function () {
	changeNum++;
	var wine_Arr_key=$('#refine option:selected').val();
	var wine_Arr_name=$('#refine option:selected').text();
	if(wine_Arr_key==""){
		getOneCategory();
	}
	if(changeNum==1){
		wxDepartment_id=wine_Arr_key;
		var departmenrt_classify={};
		departmenrt_classify.id=wine_Arr_key;
		departmenrt_classify.name=wine_Arr_name;
		wineMenu_Arr.push(departmenrt_classify);
	}else if(changeNum==2){
		wcAisle_id=wine_Arr_key;
		var asias_classify={};
		asias_classify.id=wine_Arr_key;
		asias_classify.name=wine_Arr_name;
		wineMenu_Arr.push(asias_classify);
	}else if(changeNum==3){
		changeNum=3;
		shelf_classify=wine_Arr_key;
		var shelf_classify={};
		shelf_classify.id=wine_Arr_key;
		shelf_classify.name=wine_Arr_name;
		wineMenu_Arr.push(shelf_classify);
	}
	sessionStorage.setItem('wineMenu_Arr',JSON.stringify(wineMenu_Arr));


	var select_option_str='';
	for(var i=0;i<wineDas.WCDepartment.length;i++){
		if(wxDepartment_id==wineDas.WCDepartment[i].id){
			var wcAisle=wineDas.WCDepartment[i].WCAisle;
			for(var j=0;j<wcAisle.length;j++){
				if(wcAisle_id==""){
					select_option_str+='<option value="'+wcAisle[j].id+'">'+wcAisle[j].Name+'</option>';
				}else if(wcAisle_id==wcAisle[j].id){
					var wcShelf=wcAisle[j].WCShelf;
					select_option_str='';
					for(var k=0;k<wcShelf.length;k++){
						select_option_str+='<option value="'+wcShelf[k].id+'">'+wcShelf[k].Name+'</option>';
					}
				}
			}
		}
		$('#refine').html('<option value="">Refine</option>'+select_option_str);
	}
	$('.allWine .content.vertical-list ul').html('');
	getCategory_sub();
	getAllWine(1);
})

//排序
$('#sortBy').change(function(){
	sort=$('#sortBy option:selected').val();
	$('.allWine .content.vertical-list ul').html('');
	getAllWine(1);
})

$('.GreenHeader .btnBox a').on('tap',function(){
	var index=$(this).index();
	$('.GreenHeader .btnBox a').removeClass('active');
	$(this).addClass('active');
	$('.mainContent .page').hide();
	$('.mainContent .page').eq(index).show();
	sessionStorage.setItem('wine_index',index);

	//mui('#offCanvasContentScroll').scroll().scrollTo(0,0,100);
	//$('#offCanvasContentScroll .mui-scroll').css('transform', 'translate3d(0px, 0px, 0px) translateZ(0px)');
})
$('.learn-about a.mui-btn').on('tap',function(){
	location.href=MPJ.base+"/pages/wine/learn-about.jsp";
})

mui.init({
	pullRefresh: {
		container: '#offCanvasContentScroll',
		up: {
			contentrefresh: '正在加载...',
			callback: pullupRefresh
		}
	}
});

/**
 * 上拉加载具体业务实现
 */
function pullupRefresh() {
	setTimeout(function() {
		mui('#offCanvasContentScroll').pullRefresh().endPullupToRefresh((noDatePager==false)); //参数为true代表没有更多数据了。
		if(noDatePager){
			pager++;
			getAllWine(pager);
		}
	}, 1000);
}
